<?php
namespace Orx\Mailer\MailBox\Message\Background;
/**
 * This class contain Static method to facilate
 * messages background action handling
 * 
 * 
 */
class Process {
	
	/**
	 * Process Message Banishment
	 * The message will be deleted and replace in the user inbox 
	 * if mailbox is in sync mode
	 * 
	 * @param array|string $messageIds
	 * 
	 */
	public static function ban($messageIds) {
		if(!is_array($messageIds)) {
			$messageIds = array($messageIds);
		}
		$nbNessages = 0;
		foreach($messageIds as $messageid) {
			if(empty($messageid)) {
				continue;
			}
			try {
				$message = \Fox\Dao::load(\Orx\Mailer\MailBox\Message::className(),$messageid);
				$message->status = \Orx\Mailer\MailBox\Message::STATUS_REMOVED;
				$message->delete();
				if($message->mailbox()->isInSyncMode()) {
					$box = $message->mailbox()->box();
					$hasBeenMoved = false;
					try {
						$boxHandler = $box->handle();
						// Select Application Folder only the message was archived
						//@TODO test if the message has been archived
						$boxHandler->selectFolder(_APPLICATION_EMAIL_FOLDER_);	
			   			// Find in the folder the current message
			   			foreach($boxHandler as $index => $currentMessage) {
			   				if($currentMessage->headerExists('message-id')) {
								$extId = $currentMessage->getHeader('message-id');
							} else {
								continue;
							}
			   				if($extId == $message->externalid) {
			   					$boxHandler->moveMessage($index,'INBOX');
			   					$hasBeenMoved = true;
			   					break;
			   				}
			   			}
					} catch(\Exception $e) {
						// Directory Hive Does not Exist or if any issue occurs
						// Create an entry in the log
					}
					if($hasBeenMoved) {
						//\Fox\Message::Success('Msg Moved');
					}
				}
				$nbNessages++;
			} catch(\Fox\Object\Exception\NotExist $e) {
				continue;
			} catch(Fox\Object\Exception\Save $e) {
				\Fox\Message::Error($e);
			}
		}
		return $nbNessages;
	}
	
	/**
	 * 
	 * Move to a specify folder
	 */
	public static function move($messageIds,$targetFolder = null,$targetStatus = null) {
		if(!is_array($messageIds)) {
			$messageIds = array($messageIds);
		}
		$nbNessages = 0;
		foreach($messageIds as $messageid) {
			if(empty($messageid)) {
				continue;
			}
			try {
				$message = \Fox\Dao::load(\Orx\Mailer\MailBox\Message::className(),$messageid);
				$message->status = \Orx\Mailer\MailBox\Message::STATUS_REMOVED;
				$message->delete();
				if($message->mailbox()->isInSyncMode()) {
					$box = $message->mailbox()->box();
					$hasBeenMoved = false;
					try {
						$boxHandler = $box->handle();
						// Select Application Folder only the message was archived
						//@TODO test if the message has been archived
						$boxHandler->selectFolder(_APPLICATION_EMAIL_FOLDER_);	
			   			// Find in the folder the current message
			   			foreach($boxHandler as $index => $currentMessage) {
			   				if($currentMessage->headerExists('message-id')) {
								$extId = $currentMessage->getHeader('message-id');
							} else {
								continue;
							}
			   				if($extId == $message->externalid) {
			   					$boxHandler->moveMessage($index,'INBOX');
			   					$hasBeenMoved = true;
			   					break;
			   				}
			   			}
					} catch(\Exception $e) {
						// Directory Hive Does not Exist or if any issue occurs
						// Create an entry in the log
					}
					if($hasBeenMoved) {
						//\Fox\Message::Success('Msg Moved');
					}
				}
				$nbNessages++;
			} catch(\Fox\Object\Exception\NotExist $e) {
				continue;
			} catch(Fox\Object\Exception\Save $e) {
				\Fox\Message::Error($e);
			}
		}
		return $nbNessages;
	}
}